package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.api.dto.AddressDTO;
import com.ruicar.afs.cloud.basic.api.dto.RelationDTO;
import com.ruicar.afs.cloud.basic.api.dto.RelationInfoChangeDTO;
import com.ruicar.afs.cloud.basic.common.entity.BasicCustAddressDetails;
import com.ruicar.afs.cloud.basic.common.entity.BasicCustBaseInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicCustContactDetails;
import com.ruicar.afs.cloud.basic.common.service.BasicCustAddressDetailsService;
import com.ruicar.afs.cloud.basic.common.service.BasicCustBaseInfoService;
import com.ruicar.afs.cloud.basic.common.service.BasicCustContactDetailsService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContactTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.PersonRoleTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 联系方式、地址信息数据更新
 */
@Slf4j
@RestController
@RequestMapping("/basicRelation")
@AllArgsConstructor
public class BasicRelationController {

    private final BasicCustContactDetailsService basicCustContactDetailsService;
    private final BasicCustAddressDetailsService basicCustAddressDetailsService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;

    @ApiOperation(value = "联系方式、地址信息数据更新")
    @PostMapping("/updateRelation")
    public IResponse updateRelation(@RequestBody RelationInfoChangeDTO dto) {
        //更新联系信息
        for (RelationDTO relation : dto.getRelations()) {
            if (EmptyUtils.isEmpty(relation.getContactNo())) {
                continue;
            }
            BasicCustContactDetails basicCustContactDetails = basicCustContactDetailsService.getOne(Wrappers.<BasicCustContactDetails>lambdaUpdate()
                    .eq(BasicCustContactDetails::getCustNo, relation.getCustNo())
                    .eq(BasicCustContactDetails::getContactType, relation.getContactType()));
            if (EmptyUtils.isEmpty(basicCustContactDetails)) {
                BasicCustContactDetails details = new BasicCustContactDetails();
                BeanUtils.copyProperties(relation, details);
                basicCustContactDetailsService.save(details);
            } else {
                basicCustContactDetails.setContactNo(relation.getContactNo());
                basicCustContactDetailsService.updateById(basicCustContactDetails);
            }
            //更新客户基础主表信息的手机号码
            if (relation.getContactType().equals(ContactTypeEnum.PHONE)) {
                BasicCustBaseInfo basicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaUpdate()
                        .eq(BasicCustBaseInfo::getCustNo, relation.getCustNo()));
                if (EmptyUtils.isNotEmpty(relation.getContactNo())) {
                    basicCustBaseInfo.setPhone(relation.getContactNo());
                }
                basicCustBaseInfoService.updateById(basicCustBaseInfo);
            }
        }

        //更新地址信息
        for (AddressDTO address : dto.getAddress()) {
            BasicCustAddressDetails basicCustAddressDetails = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>lambdaUpdate()
                    .eq(BasicCustAddressDetails::getCustNo, address.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, address.getAddressType()));
            if (EmptyUtils.isEmpty(basicCustAddressDetails)) {
                BasicCustAddressDetails details = new BasicCustAddressDetails();
                BeanUtils.copyProperties(address, details);
                basicCustAddressDetailsService.save(details);
            } else {
                if (EmptyUtils.isNotEmpty(address.getProvince())) {
                    basicCustAddressDetails.setProvince(address.getProvince());
                }
                if (EmptyUtils.isNotEmpty(address.getCity())) {
                    basicCustAddressDetails.setCity(address.getCity());
                }
                if (EmptyUtils.isNotEmpty(address.getCounty())) {
                    basicCustAddressDetails.setCounty(address.getCounty());
                }
                if (EmptyUtils.isNotEmpty(address.getStreet())) {
                    basicCustAddressDetails.setStreet(address.getStreet());
                }
                if (EmptyUtils.isNotEmpty(address.getAddress())) {
                    basicCustAddressDetails.setAddress(address.getAddress());
                }
                basicCustAddressDetailsService.updateById(basicCustAddressDetails);
            }

        }
        return new IResponse().success("更新成功");
    }

}
